package com.yunqiao.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunqiao.common.utils.ApiResponse;
import com.yunqiao.common.utils.R;
import com.yunqiao.service.api.AppVersionService;
import com.yunqiao.service.api.UserService;
import com.yunqiao.service.api.entity.AppVersionEntity;
import com.yunqiao.service.api.entity.UserEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/app")
public class AppUpdateApiController {
    @Resource
    private AppVersionService appVersionService;
    @Resource
    private UserService userService;
    @RequestMapping("update")
    public ApiResponse getUpdateVersion(AppVersionEntity appVersionEntity){
        AppVersionEntity version=appVersionService.getOne(new QueryWrapper<AppVersionEntity>().eq("name",appVersionEntity.getName()).ne("version",appVersionEntity.getVersion()));
        if(version!=null&&appVersionEntity.getUserId()!=null){
            UserEntity userEntity=new UserEntity();
            userEntity.setAppVersion(version.getVersion());
            userEntity.setId(appVersionEntity.getUserId());
            userService.updateById(userEntity);
        }
        if(!version.equals(appVersionEntity.getVersion())){
            return new ApiResponse(version);
        }else{
            return new ApiResponse();
        }
    }
}
